package kr.hkit.powerjava.chapter15.menu;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import java.awt.Font;
import javax.swing.JMenuItem;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class TestMain extends JFrame implements ActionListener {

	private JPanel contentPane;
	private JMenuBar menuBar;
	private JMenu mnNewMenu;
	private JMenuItem addItem;
	private JMenuItem updateItem;
	private JMenuItem deleteItem;
	private JMenuItem listItems;
	private JMenu mnNewMenu_1;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TestMain frame = new TestMain();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public TestMain() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		
		menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		mnNewMenu = new JMenu("사원 정보");
		mnNewMenu.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
		menuBar.add(mnNewMenu);
		
		addItem = new JMenuItem("추가");
		addItem.addActionListener(this);
		mnNewMenu.add(addItem);
		
		updateItem = new JMenuItem("수정");
		updateItem.addActionListener(this);
		mnNewMenu.add(updateItem);
		
		deleteItem = new JMenuItem("삭제");
		mnNewMenu.add(deleteItem);
		
		listItems = new JMenuItem("리스트");
		mnNewMenu.add(listItems);
		
		mnNewMenu_1 = new JMenu("부서 정보");
		menuBar.add(mnNewMenu_1);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == updateItem) {
			actionPerformedUpdateItem(e);
		}
		if (e.getSource() == addItem) {
			actionPerformedAddItem(e);
		}
		contentPane.revalidate();//repaint는 안 먹힘 이걸로 해야 refresh해줌
	}
	protected void actionPerformedAddItem(ActionEvent e) {
		InputEmp emp = new InputEmp();
		emp.setLblNewLabel("사원 정보 추가");
		contentPane.removeAll();
		contentPane.add(emp, BorderLayout.CENTER);

	}
	protected void actionPerformedUpdateItem(ActionEvent e) {
		InputEmp emp = new InputEmp();
		emp.setLblNewLabel("사원 정보 수정");
		contentPane.removeAll();//안해주면 추가 후 수정 시 수정으로 나오지 않음
		contentPane.add(emp, BorderLayout.CENTER);

	}
}
